package com.axzh.interview.exam;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AliSecond {
	
	public static int Max = 2000000;
	public static Map<String,Integer> map = new HashMap<>();
	
	public static void main(String[] args) {
		
		ExecutorService exec = Executors.newCachedThreadPool();
		Random rd = new Random(20);
		int count = 1000;
		for(int i=0;i<2000;i++) {
			count = rd.nextInt(20) * 1000;
			if(count>=1000) {
				exec.execute(new User("用户"+i, count));
			}
			if(Max==0)
				break;
		}
		exec.shutdown();
		
		for(Map.Entry<String, Integer> entry : map.entrySet()) {
			System.out.println(entry.getKey()+"购买了"+entry.getValue()+"金额的'万能险'产品");
		}
	}

	private static class User implements Runnable {

		private String name;
		private int count;
		public User(String name, int count) {
			this.name = name;
			this.count = count;
		}
		@Override
		public void run() {
			synchronized (this) {
				if(!Thread.interrupted() && Max-count>=0) {
					Max = Max-count;
					map.put(name, count);
				}
			}
		}
	}
}

